package com.ruoyi.settled.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.settled.domain.ProductOrderCost;
import com.ruoyi.settled.mapper.SalesOrderCostMapper;
import com.ruoyi.settled.domain.SalesOrderCost;
import com.ruoyi.settled.service.ISalesOrderCostService;

/**
 * 成本核算Service业务层处理
 * 
 * @author huangchen
 * @date 2025-03-14
 */
@Service
public class SalesOrderCostServiceImpl implements ISalesOrderCostService 
{
    @Autowired
    private SalesOrderCostMapper salesOrderCostMapper;

    /**
     * 查询成本核算
     * 
     * @param costId 成本核算主键
     * @return 成本核算
     */
    @Override
    public SalesOrderCost selectSalesOrderCostByCostId(Long costId)
    {
        return salesOrderCostMapper.selectSalesOrderCostByCostId(costId);
    }

    /**
     * 查询成本核算列表
     * 
     * @param salesOrderCost 成本核算
     * @return 成本核算
     */
    @Override
    public List<SalesOrderCost> selectSalesOrderCostList(SalesOrderCost salesOrderCost)
    {
        return salesOrderCostMapper.selectSalesOrderCostList(salesOrderCost);
    }

    /**
     * 新增成本核算
     * 
     * @param salesOrderCost 成本核算
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSalesOrderCost(SalesOrderCost salesOrderCost)
    {
        int rows = salesOrderCostMapper.insertSalesOrderCost(salesOrderCost);
        insertProductOrderCost(salesOrderCost);
        return rows;
    }

    /**
     * 修改成本核算
     * 
     * @param salesOrderCost 成本核算
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSalesOrderCost(SalesOrderCost salesOrderCost)
    {
        salesOrderCostMapper.deleteProductOrderCostByCostId(salesOrderCost.getCostId());
        insertProductOrderCost(salesOrderCost);
        return salesOrderCostMapper.updateSalesOrderCost(salesOrderCost);
    }

    /**
     * 批量删除成本核算
     * 
     * @param costIds 需要删除的成本核算主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesOrderCostByCostIds(Long[] costIds)
    {
        salesOrderCostMapper.deleteProductOrderCostByCostIds(costIds);
        return salesOrderCostMapper.deleteSalesOrderCostByCostIds(costIds);
    }

    /**
     * 删除成本核算信息
     * 
     * @param costId 成本核算主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesOrderCostByCostId(Long costId)
    {
        salesOrderCostMapper.deleteProductOrderCostByCostId(costId);
        return salesOrderCostMapper.deleteSalesOrderCostByCostId(costId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param salesOrderCost 成本核算对象
     */
    public void insertProductOrderCost(SalesOrderCost salesOrderCost)
    {
        List<ProductOrderCost> productOrderCostList = salesOrderCost.getProductOrderCostList();
        Long costId = salesOrderCost.getCostId();
        if (StringUtils.isNotNull(productOrderCostList))
        {
            List<ProductOrderCost> list = new ArrayList<ProductOrderCost>();
            for (ProductOrderCost productOrderCost : productOrderCostList)
            {
                productOrderCost.setCostId(costId);
                list.add(productOrderCost);
            }
            if (list.size() > 0)
            {
                salesOrderCostMapper.batchProductOrderCost(list);
            }
        }
    }
}
